
import copy

def demo1():
    """深拷贝 与 浅拷贝"""
    a = [11,22]
    b = [33,44]
    c = [a,b]
    d = c
    e = copy.copy(c) # 浅拷贝
    f = copy.deepcopy(c) # 深拷贝

    print(id(a))
    print(id(b))
    print(c)
    print(id(c))
    print(id(d))
    print(id(e))

    # 浅拷贝
    print(id(c[0]))
    print(id(e[0]))

    # 深拷贝
    print(id(f[0]))

    print(c)
    a.append(66)
    print(c)
    print(d) # 浅拷贝
    print(e) # 浅拷贝
    print(f) # 深拷贝

def demo2():
    """引用 与 浅拷贝"""
    a = [11,22]
    b = [33,44]
    c = [a,b]
    d = c # 引用
    e = copy.copy(c) # 浅拷贝

    print(c)
    c.append(55)
    print(c)
    print(d)
    print(e)

def demo3():
    """拷贝元组"""
    # 当元组内均为基本类型时，拷贝元组时不生效 因为元组是不可变类型
    a = (11,22)
    b = copy.copy(a)
    c = copy.deepcopy(a)
    print(id(a))
    print(id(b))
    print(id(c))

    a += (33,)
    print(a)
    print(id(a))
    print(b)
    print(id(b))
    print(c)
    print(id(c))

    a = [11,22]
    b = [33,44]
    c = (a,b)
    d = copy.copy(c)
    e = copy.deepcopy(c)

    print("c: " + str(id(c)))
    print("d: " + str(id(d)))
    print("e: " + str(id(e)))

    a.append(55)
    print(c)
    print(d)
    print(e)

def demo4():
    a = dict(name = "kobe", age = 27, children_ages = [11,13])
    b = a.copy()
    c = copy.copy(a)
    d = copy.deepcopy(a)
    print(id(a))
    print(id(b))
    print(id(c))
    print(id(d))

    # 经拷贝后，基础类型不会随来源的改变而改变
    a['children_ages'].append(16)
    a['age'] = 28
    print(a)
    print(b)
    print(c)
    print(d)
    print(id(a))
    print(id(b))
    print(id(c))
    print(id(d))

def main():
    demo4()

if __name__ == '__main__':
    main()